// 分页指令
define('paginationDirective', ['angular'], function (angular) {
    var m = angular.module('directives');
    m.directive('pagination', [function () {
        return {
            restrict: 'AE',
            templateUrl: 'js/templates/pagination.html',
            replace: true,
            scope:{
                allData:"@",
                currentData:"="
            },
            controller:['$scope',function($scope){
                $scope.myPage={
                    currentPage:1,//访问第几页数据，从1开始
                    totalItems:0,//数据库中总共有多少条数据
                    itemsPerPage: 15,//默认每页展示多少条数据，可更改
                    pagesLength: 15
        　　　　};
                $scope.initPageList=function(){
                    // 初始化总条数
                    $scope.allDataArr=angular.fromJson($scope.allData);
                    // $scope.allDataArr=$scope.allData;
                    $scope.myPage.totalItems=$scope.allDataArr.length;
                    // 初始化页码
                    var list=[];
                    var totalPage=Math.ceil($scope.myPage.totalItems/$scope.myPage.itemsPerPage);
                    for (var i = 1; i <= totalPage; i++) {
                        list[i-1]=i;
                    }
                    // 页面上的页码数组
                    $scope.pageList=list;
                    $scope.myPage.currentPage=1;
                    $scope.initCurrntData();
                };
                $scope.initCurrntData=function(){
                    // 默认一页显示的条数*当前页面数-1
                    var start=$scope.myPage.itemsPerPage*($scope.myPage.currentPage-1);
                    var end=$scope.myPage.itemsPerPage*($scope.myPage.currentPage);
                    $scope.currentData=$scope.allDataArr.slice(start,end);
                };
                $scope.$watch("allData",function(n,o){
                    $scope.initPageList();
        　　　　});
                // 前一页
                $scope.previous=function(){
                    $scope.myPage.currentPage=Math.max(--$scope.myPage.currentPage,1);
                    $scope.initCurrntData();
                };
                // 后一页
                $scope.next=function(){
                    $scope.myPage.currentPage=Math.min(++$scope.myPage.currentPage,Math.ceil($scope.myPage.totalItems/$scope.myPage.itemsPerPage));
                    $scope.initCurrntData();
                };
                // 跳转某一页
                $scope.selectPage=function(i){
                    $scope.myPage.currentPage=i;
                    $scope.initCurrntData();
                };
                $scope.isActivePage=function(i){
                    return i==$scope.myPage.currentPage?true:false;
                };
            }]
        };
    }]);
});